package com.summer.applet.entity;

import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;

import com.summer.applet.entity.GameDirector.Node;

/**
 * 蛇
 * @author summer_xx
 */
public class Snake implements Node{
	
	public static final int DIRECTION_LEFT	= 1;
	public static final int DIRECTION_RIGHT	= 2;
	public static final int DIRECTION_UP	= 3;
	public static final int DIRECTION_DOWN	= 4;
	
	private boolean lifeState = true;
	private int direction=DIRECTION_RIGHT;
	private Point location = new Point(30,30);
	private int size = 10;
	private ArrayList<Point> nodeList = new ArrayList<Point>();
	
	public Snake() {
		nodeList.add(location);
		nodeList.add(location);
		nodeList.add(location);
	}
	
	public void draw(Graphics graphics) {
		for(Point nodeLocation : nodeList) {
			graphics.drawRect(nodeLocation.x, nodeLocation.y, size, size);
		}
	}
	
	public void directionAction(int direction) {
		this.direction = direction;
	}
	
	public void runAction() {
		switch (direction) {
			case DIRECTION_LEFT:
				location.x -= size;
				break;
			case DIRECTION_RIGHT:
				location.x += size;
				break;
			case DIRECTION_UP:
				location.y -= size;
				break;
			case DIRECTION_DOWN:
				location.y += size;
				break;
		}
		nodeList.add(0, new Point(location));
		nodeList.remove(nodeList.size()-1);
	}
	
	public void eatAction(Flower flower) {
		if(flower.getLocation().equals(location)) {
			nodeList.add(0, new Point(location));
			flower.rebirth();
		}
	}
	
	public Point getLocation() {
		return location;
	}
	
	public void setLifeState(boolean lifeState) {
		this.lifeState = lifeState;
	}
	public boolean lifeState() {
		return lifeState;
	}
}
